package com.lry.jvm.instructions.base;

import com.lry.jvm.rtda.Frame;
import com.lry.jvm.rtda.JThread;
import com.lry.jvm.rtda.Slot;
import com.lry.jvm.rtda.heap.JMethod;
import com.lry.jvm.util.Util;

public class MethodInvoke {

    public static void invokeMethod(Frame frame, JMethod method){
        JThread thread = frame.getThread();

        //被调用的方法的帧
        Frame newFrame = thread.newFrame(method);
        thread.pushFrame(newFrame);

        int argsSlotCount = method.getArgsSlotCount();
        if(argsSlotCount>0){
            for(int i=argsSlotCount-1;i>=0;i--){
                //从当前帧弹出参数个slot
                Slot slot = frame.getOperandStack().popSlot();
                //放到新帧的局部变量表
                newFrame.getLocalVars().setSlot(i,slot);
            }
        }

        if(method.isNative()){
            if(method.getName().equals("registerNatives")){
                thread.popFrame();
            }else {
                Util.panic("not support native");
            }
        }
    }
}
